<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button id="btn">
        复制
    </button>
    <script>
        let index = 1
        btn.addEventListener('click', () => {
            // 浏览器适配模式 
            if (navigator.clipboard) {
                var text = '需要复制的内容' + index++
                navigator.clipboard.writeText(text)
            } else {
                var textarea = document.createElement('textarea')
                document.body.appendChild(textarea);
                textarea.style.position = 'absolute';
                textarea.style.clip = 'rect(0 0 0 0)'
                textarea.value = 'copy的内容' + index++
                textarea.select()
                document.execCommand('copy');
            }


        })

    </script>
</body>

</html>